package libs

import (
	"github.com/duanshanghanqing/godash"
	"github.com/spf13/viper"
	"log"
	"path"
)

type App struct {
	Name        string `yaml:"name" mapstructure:"name"`
	IP          string `yaml:"ip" mapstructure:"ip"`                   // 服务ip, 程序自动获取主机ip
	Port        int    `yaml:"port" mapstructure:"port"`               // 主服务端口
	GatewayPort int    `yaml:"gatewayPort" mapstructure:"gatewayPort"` // 代理主服务的 http api gateway 端口 = 主服务端口 + 1
	BizPort     int    `yaml:"bizPort" mapstructure:"bizPort"`         // 代理业务端口 = 主服务端口 + 2
	Env         string `yaml:"env" mapstructure:"env"`
}

var AppConfig App

type Log struct {
	Level      string `yaml:"level" mapstructure:"level"`
	FileName   string `yaml:"filename" mapstructure:"filename"`
	MaxSize    int    `yaml:"max_size" mapstructure:"max_size"`
	MaxAge     int    `yaml:"max_age" mapstructure:"max_age"`
	MaxBackups int    `yaml:"max_backups" mapstructure:"max_backups"`
}

var LogConfig Log

type Mysql struct {
	Host     string `yaml:"host" mapstructure:"host"`
	Port     int    `yaml:"port" mapstructure:"port"`
	UserName string `yaml:"username" mapstructure:"username"`
	Password string `yaml:"password" mapstructure:"password"`
	DBname   string `yaml:"dbname" mapstructure:"dbname"`
}

var MysqlConfig Mysql

type Redis struct {
	Addr     string `yaml:"addr" mapstructure:"addr"`
	Password string `yaml:"password" mapstructure:"password"`
}

var RedisConfig Redis

type Rocketmq struct {
	Endpoint     string `yaml:"endpoint" mapstructure:"endpoint"`
	AccessKey    string `yaml:"accessKey" mapstructure:"accessKey"`
	AccessSecret string `yaml:"accessSecret" mapstructure:"accessSecret"`
}

var RocketmqConfig Rocketmq

type Consul struct {
	Address string `yaml:"address" mapstructure:"address"`
	Port    int    `yaml:"port" mapstructure:"port"`
}

var ConsulConfig Consul

type Tracer struct {
	Host string `yaml:"host" mapstructure:"host"`
	Port int    `yaml:"port" mapstructure:"port"`
}

var TracerConfig Tracer

// 主要用于配置文件映射到结构体
type config struct {
	App      App      `yaml:"app"`
	Log      Log      `yaml:"log"`
	Mysql    Mysql    `yaml:"mysql"`
	Redis    Redis    `yaml:"redis"`
	Rocketmq Rocketmq `yaml:"rocketmq"`
	Consul   Consul   `yaml:"consul"`
	Tracer   Tracer   `yaml:"tracer"`
}

func configInit(env string) error {
	rootPath, rootPathErr := godash.GetRootPath(AppConfig.Name)
	if rootPathErr != nil {
		return rootPathErr
	}

	v := viper.New()
	//设置配置文件类型，可选
	v.SetConfigType("yaml")

	// 本地环境
	filename := "config." + env + ".yaml"
	v.SetConfigFile(path.Join(rootPath, filename))

	// 读取配置文件
	if err := v.ReadInConfig(); err != nil {
		log.Fatalf("读取配置失败, %v", err)
		return err
	}

	var c config
	if err := v.Unmarshal(&c); err != nil {
		log.Fatalf("序列化配置失败, %v", err)
		return err
	}
	c.App.Env = env
	c.App.Name = AppConfig.Name
	ip, err := godash.GetIPv4()
	if err != nil {
		return err
	}
	c.App.IP = ip.String() // 本机ip，服务启动监听的是 0.0.0.0 本地
	if c.App.Port == 0 {   // 没有设置端口号，就使用动态端口
		port, err := godash.GetFreePort()
		if err != nil {
			return err
		}
		c.App.Port = port
	}
	c.App.GatewayPort = c.App.Port + 1
	c.App.BizPort = c.App.Port + 2

	AppConfig = c.App
	LogConfig = c.Log
	MysqlConfig = c.Mysql
	RedisConfig = c.Redis
	RocketmqConfig = c.Rocketmq
	ConsulConfig = c.Consul
	TracerConfig = c.Tracer

	//fmt.Println(AppConfig)
	//fmt.Println(LoggerConfig)
	//fmt.Println(MysqlConfig)
	//fmt.Println(RedisConfig)
	//fmt.Println(ConsulConfig)

	//监听配置文件改变
	//v.WatchConfig()

	//v.OnConfigChange(func(in fsnotify.Event) {
	//	//配置文件改变
	//	if err := v.ReadInConfig(); err != nil {
	//		log.Fatalf("v.ReadInConfig()%s", err.Error())
	//	}
	//})

	return nil
}
